package services

import (
	"game-simple/internal/framework/system/models"

	"gorm.io/gorm"
)

// SeedSequenceRules 初始化编号规则种子数据（需要传入 db 和 logger）
func SeedSequenceRules(db *gorm.DB, logInfof func(string, ...interface{}), logInfo func(string)) error {

	// 检查是否已有数据
	var count int64
	if err := db.Model(&models.SequenceRule{}).Count(&count).Error; err != nil {
		return err
	}

	if count > 0 {
		if logInfo != nil {
			logInfo("编号规则种子数据已存在，跳过初始化")
		}
		return nil
	}

	rules := []models.SequenceRule{
		{
			Code:        "ROOM",
			Name:        "房间编号",
			Prefix:      "RM",
			DateFormat:  "YYYYMMDD",
			SequenceLen: 6,
			ResetType:   "daily",
			CurrentSeq:  0,
			Status:      "active",
			Description: "房间编号规则，每日重置",
		},
		{
			Code:        "ORDER",
			Name:        "订单编号",
			Prefix:      "ORD",
			DateFormat:  "YYYYMMDD",
			SequenceLen: 6,
			ResetType:   "daily",
			CurrentSeq:  0,
			Status:      "active",
			Description: "订单编号规则，每日重置",
		},
		{
			Code:        "INVOICE",
			Name:        "发票编号",
			Prefix:      "INV",
			DateFormat:  "YYYYMM",
			SequenceLen: 4,
			ResetType:   "monthly",
			CurrentSeq:  0,
			Status:      "active",
			Description: "发票编号规则，每月重置",
		},
		{
			Code:        "SERIAL",
			Name:        "流水号",
			Prefix:      "SR",
			DateFormat:  "YYYYMMDD",
			SequenceLen: 8,
			ResetType:   "never",
			CurrentSeq:  0,
			Status:      "active",
			Description: "流水号规则，从不重置",
		},
	}

	// 批量创建
	if err := db.Create(&rules).Error; err != nil {
		if logInfof != nil {
			logInfof("初始化编号规则种子数据失败: %v", err)
		}
		return err
	}

	if logInfof != nil {
		logInfof("编号规则种子数据初始化成功，共 %d 条规则", len(rules))
	}
	return nil
}

// SeedSequenceRulesIfNotExists 如果不存在则初始化种子数据（幂等操作）
func SeedSequenceRulesIfNotExists(db *gorm.DB) error {

	rules := []struct {
		Code        string
		Name        string
		Prefix      string
		DateFormat  string
		SequenceLen int
		ResetType   string
		Description string
	}{
		{
			Code:        "ROOM",
			Name:        "房间编号",
			Prefix:      "RM",
			DateFormat:  "YYYYMMDD",
			SequenceLen: 6,
			ResetType:   "daily",
			Description: "房间编号规则，每日重置",
		},
		{
			Code:        "ORDER",
			Name:        "订单编号",
			Prefix:      "ORD",
			DateFormat:  "YYYYMMDD",
			SequenceLen: 6,
			ResetType:   "daily",
			Description: "订单编号规则，每日重置",
		},
		{
			Code:        "INVOICE",
			Name:        "发票编号",
			Prefix:      "INV",
			DateFormat:  "YYYYMM",
			SequenceLen: 4,
			ResetType:   "monthly",
			Description: "发票编号规则，每月重置",
		},
		{
			Code:        "SERIAL",
			Name:        "流水号",
			Prefix:      "SR",
			DateFormat:  "YYYYMMDD",
			SequenceLen: 8,
			ResetType:   "never",
			Description: "流水号规则，从不重置",
		},
	}

	for _, ruleData := range rules {
		var existing models.SequenceRule
		result := db.Where("code = ?", ruleData.Code).First(&existing)

		if result.Error == gorm.ErrRecordNotFound {
			// 不存在，创建新规则
			rule := models.SequenceRule{
				Code:        ruleData.Code,
				Name:        ruleData.Name,
				Prefix:      ruleData.Prefix,
				DateFormat:  ruleData.DateFormat,
				SequenceLen: ruleData.SequenceLen,
				ResetType:   ruleData.ResetType,
				CurrentSeq:  0,
				Status:      "active",
				Description: ruleData.Description,
			}
			if err := db.Create(&rule).Error; err != nil {
				return err
			}
		} else if result.Error != nil {
			return result.Error
		}
		// 如果已存在，跳过
	}

	return nil
}

